/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIAsignarPallets.java
 *
 * Created on 27/10/2011, 01:35:45 PM
 */

package GUI.Pallet;
import BE.BEContacto;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import BE.BEPaleta;
import BE.BEPallet;
import BE.BEProducto;
import BL.BLPaleta;
import BL.BLPallet;
import BL.BLProducto;
import GUI.Paleta.GUIMantenimientoPaleta;
import GUI.Productos.GUIMantenimientoProducto;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Date;
import javax.swing.JOptionPane;
public class GUIAsignarPallets extends javax.swing.JDialog {
    private BEProducto objproducto;
    private BEPaleta objpaleta;
    private BEPallet objpallet;

/**
 *
 * @author Cristian
 */
    private BEContacto objContacto;
    /** Creates new form GUIAsignarPallets */
    public GUIAsignarPallets(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
       
        initComponents();
        this.setLocationRelativeTo(null);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int height = screenSize.height;
        int width = screenSize.width;
        this.setLocation(width/2-this.getWidth()/2, height/2-this.getHeight()/2);

    }

    public BEPallet getObjpallet() {
        return objpallet;
    }

    public void setObjpallet(BEPallet objpallet) {
        this.objpallet = objpallet;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        pnlProducto = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtProducto = new javax.swing.JTextField();
        btnProducto = new javax.swing.JButton();
        lbDescProducto = new javax.swing.JLabel();
        txtDescripcionProducto = new javax.swing.JTextField();
        fechaVencimiento = new com.toedter.calendar.JDateChooser();
        jLabel3 = new javax.swing.JLabel();
        pnlPaleta = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtPaleta = new javax.swing.JTextField();
        btnPaleta = new javax.swing.JButton();
        lbDescripcionPaleta = new javax.swing.JLabel();
        txtDescripcionPaleta = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        btnCancelar = new javax.swing.JButton();
        btnAgregar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Registrar Pallet");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Palleta"));

        pnlProducto.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Producto"));

        jLabel1.setText("Producto:");

        txtProducto.setEditable(false);
        txtProducto.setEnabled(false);

        btnProducto.setText("...");
        btnProducto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProductoActionPerformed(evt);
            }
        });

        lbDescProducto.setText("Descripcion:");

        txtDescripcionProducto.setEnabled(false);

        jLabel3.setText("Fecha Vencimiento:");

        javax.swing.GroupLayout pnlProductoLayout = new javax.swing.GroupLayout(pnlProducto);
        pnlProducto.setLayout(pnlProductoLayout);
        pnlProductoLayout.setHorizontalGroup(
            pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlProductoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbDescProducto)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnlProductoLayout.createSequentialGroup()
                        .addComponent(txtProducto, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txtDescripcionProducto, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                    .addComponent(fechaVencimiento, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlProductoLayout.setVerticalGroup(
            pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlProductoLayout.createSequentialGroup()
                .addGroup(pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnlProductoLayout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addGroup(pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtProducto)
                            .addComponent(btnProducto)))
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbDescProducto)
                    .addComponent(txtDescripcionProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(fechaVencimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap())
        );

        pnlPaleta.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Paleta"));

        jLabel2.setText("Paleta:");

        txtPaleta.setEditable(false);
        txtPaleta.setEnabled(false);

        btnPaleta.setText("...");
        btnPaleta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPaletaActionPerformed(evt);
            }
        });

        lbDescripcionPaleta.setText("Descripcion:");

        txtDescripcionPaleta.setEnabled(false);

        javax.swing.GroupLayout pnlPaletaLayout = new javax.swing.GroupLayout(pnlPaleta);
        pnlPaleta.setLayout(pnlPaletaLayout);
        pnlPaletaLayout.setHorizontalGroup(
            pnlPaletaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPaletaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlPaletaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(lbDescripcionPaleta))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 85, Short.MAX_VALUE)
                .addGroup(pnlPaletaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(pnlPaletaLayout.createSequentialGroup()
                        .addComponent(txtPaleta, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnPaleta, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txtDescripcionPaleta))
                .addContainerGap())
        );
        pnlPaletaLayout.setVerticalGroup(
            pnlPaletaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlPaletaLayout.createSequentialGroup()
                .addGroup(pnlPaletaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPaleta)
                    .addComponent(btnPaleta, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(pnlPaletaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbDescripcionPaleta)
                    .addComponent(txtDescripcionPaleta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(pnlPaleta, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlProducto, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(pnlProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlPaleta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnCancelar.setText("Cancelar");
        btnCancelar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnCancelar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnAgregar.setText("Aceptar");
        btnAgregar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnAgregar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(190, Short.MAX_VALUE)
                .addComponent(btnAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed

        Date fecha= new Date();

        if (fechaVencimiento.getDate().compareTo(fecha)<0)
              JOptionPane.showMessageDialog(null,"La Fecha de Vencimiento debe ser posterior al dia de hoy",
                      "Informacion",JOptionPane.INFORMATION_MESSAGE);
          
        if (txtPaleta.getText().compareTo("")==0 || txtProducto.getText().compareTo("")==0
                || fechaVencimiento.toString().compareTo("")==0)   
        {
              JOptionPane.showMessageDialog(null,"Faltan completar algun dato",
                      "Informacion",JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {

            objpallet=new BEPallet();
            objpallet.setPaleta(objpaleta);
            objpallet.setIdProducto(objproducto);
            objpallet.setFechaVencimiento(fechaVencimiento.getDate());
            objpallet.setEstado("REGISTRADO");


//            objpallet.setCantidad();
            
            this.setVisible(false);
            //SimpleDateFormat formateador = new SimpleDateFormat("dd-MM-yyyy");
            //System.out.println(formateador.format(fechaVencimiento.getDate()));



            BLPallet blpallet=new BLPallet();

            System.out.println(blpallet.getCapacidad(objproducto.getIdProducto(),objpaleta.getIdTipoPallet() ));
            objpallet.setCantidad(blpallet.getCapacidad(objproducto.getIdProducto(),objpaleta.getIdTipoPallet() ));

            blpallet.agregarPallet(objpallet);
            
            BL.BLLogAudit objGestorLog= new BL.BLLogAudit();
            BE.BELogAudit auxLogAudit= new BE.BELogAudit();
            auxLogAudit.setIdInterface(0);
            auxLogAudit.setIdUsuario(BL.BLCardex.getObjUsuario().getIdUsuario());
            auxLogAudit.setAccion("Registro de Pallet por el usuario "+BL.BLCardex.getObjUsuario().getUsuario()+"." );
            java.util.Date aux= new java.util.Date();
            java.sql.Timestamp auxfecha= new java.sql.Timestamp(aux.getTime());
            
            auxLogAudit.setFecha(auxfecha);
            
            objGestorLog.registrarAccion(auxLogAudit);

            

            
             
        }
}//GEN-LAST:event_btnAgregarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

    private void btnProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProductoActionPerformed
        // TODO add your handling code here:

         GUIMantenimientoProducto frmBuscarProducto= new GUIMantenimientoProducto (null,true,objproducto);
        frmBuscarProducto.setVisible(true);

        objproducto = new BEProducto();
        objproducto= frmBuscarProducto.getObjProducto();

        if (objproducto!=null)
        {
            txtProducto.setText(objproducto.getNombre());
            txtDescripcionProducto.setText(objproducto.getDescripcion());
            
        }
        
        frmBuscarProducto.dispose();


    }//GEN-LAST:event_btnProductoActionPerformed

    private void btnPaletaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPaletaActionPerformed
        // TODO add your handling code here:
        GUIMantenimientoPaleta frmBuscarPaleta= new GUIMantenimientoPaleta (null,true,objpaleta);
        frmBuscarPaleta.setVisible(true);

        objpaleta=new BEPaleta();
        objpaleta= frmBuscarPaleta.getObjPaleta();
        if (objpaleta != null)
        {
            txtPaleta.setText(objpaleta.getNombre());
            txtDescripcionPaleta.setText(objpaleta.getDescripcion());
        }
        
        frmBuscarPaleta.dispose();



    }//GEN-LAST:event_btnPaletaActionPerformed

    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                GUIAsignarPallets dialog = new GUIAsignarPallets(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnPaleta;
    private javax.swing.JButton btnProducto;
    private com.toedter.calendar.JDateChooser fechaVencimiento;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel lbDescProducto;
    private javax.swing.JLabel lbDescripcionPaleta;
    private javax.swing.JPanel pnlPaleta;
    private javax.swing.JPanel pnlProducto;
    private javax.swing.JTextField txtDescripcionPaleta;
    private javax.swing.JTextField txtDescripcionProducto;
    private javax.swing.JTextField txtPaleta;
    private javax.swing.JTextField txtProducto;
    // End of variables declaration//GEN-END:variables

}
